﻿#include "StartDialogItemWidget.h"
#include <QGridLayout>
#include <QPalette>

StartDialogItemWidget::StartDialogItemWidget(QString name, QString desc, QPixmap img, QWidget *parent)
	: QWidget(parent)
{
	this->setAutoFillBackground(true);
	QPalette pal(this->palette());
	pal.setColor(QPalette::Background, Qt::transparent);
	this->setPalette(pal);

	m_Layout = new QGridLayout(this);
	m_Img = new QLabel;
	m_Title = new QLabel;
	m_Desc = new QLabel;

	QFont f("Arial");
	m_Img->setPixmap(img);
	m_Img->setFixedSize(QSize(32, 32));
	m_Title->setText(name);
	m_Title->setAlignment(Qt::AlignLeft);
	m_Title->setFont(f);
	m_Desc->setText(desc);
	m_Desc->setAlignment(Qt::AlignLeft);
	m_Desc->setFont(f);

	m_Layout->addWidget(m_Img, 0, 0, 2, 1);
	m_Layout->addWidget(m_Title, 0, 1, 1, 1);
	m_Layout->addWidget(m_Desc, 1, 1, 1, 1);
}


StartDialogItemWidget::~StartDialogItemWidget()
{
	if (m_Img != nullptr) {
		delete m_Img;
		m_Img = nullptr;
	}
	if (m_Title != nullptr) {
		delete m_Title;
		m_Title = nullptr;
	}
	if (m_Desc != nullptr) {
		delete m_Desc;
		m_Desc = nullptr;
	}
	if (m_Layout != nullptr) {
		delete m_Layout;
		m_Layout = nullptr;
	}
}

QString StartDialogItemWidget::getTitle() const
{
	return m_Title->text().trimmed();
}

QString StartDialogItemWidget::getDesc() const
{
	return m_Desc->text().trimmed();
}

void StartDialogItemWidget::mousePressEvent(QMouseEvent * ev)
{
	m_MousePos = QPoint(ev->x(), ev->y());
}

void StartDialogItemWidget::mouseReleaseEvent(QMouseEvent * ev)
{
	if (m_MousePos == QPoint(ev->x(), ev->y()))
	{
		emit clicked();
		emit clicked(this);
	}
}

void StartDialogItemWidget::enterEvent(QEvent * event)
{
	setCursor(Qt::PointingHandCursor);

	QPalette pal(this->palette());
	pal.setColor(QPalette::Background, QColor(237, 246, 254));
	this->setPalette(pal);
}

void StartDialogItemWidget::leaveEvent(QEvent * event)
{
	setCursor(Qt::ArrowCursor);
	QPalette pal(this->palette());
	pal.setColor(QPalette::Background, Qt::transparent);
	this->setPalette(pal);
}
